﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using GameSPK.Model;
using System.Windows.Controls;
using GameSPK.Helpers;
using System.Windows.Media;
using GameSPK.DAO;
using System.Windows.Media.Imaging;
using System.Windows;

namespace GameSPK.Start
{
    class frmSection2Start: frmGenerel
    {
        private static frmSection2Start instance;
        private static object syncLock = new object();
        int goicauhoi;
        public static frmSection2Start getInstance(int goi)
        {
            if (instance == null)
            {
                lock (syncLock)
                {
                    if (instance == null)
                    {
                        instance = new frmSection2Start(goi);
                        
                    }
                }
            }

            return instance;
        }
        private frmSection2Start(int goi)
            : base()
        {
            goicauhoi = goi;
            isfirst = true;
            this.Closed += new EventHandler(frmSection2Start_Closed);
            this.Loaded += new System.Windows.RoutedEventHandler(frmSection2Start_Loaded);
            AddHandler(Keyboard.KeyDownEvent, (System.Windows.Input.KeyEventHandler)frmSection2Start_KeyDown);
            
        }
        bool isfirst;
        void frmSection2Start_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter && isfirst)
            {
                isfirst = false;
                tbcontent.Text = cauhoi.CauHoi1.Trim();
                lblContent.Content = tbcontent;
                timerClock.Enabled = true;
                timerClock.Start();
            }
            else if (e.Key == Key.Right && !isfirst)
            {
                cauhoi = cauhoi = CauHoiDAO.getCauHoiTrinhChieu(MainWindow.getCuocThiChon(), 2, goicauhoi);
                if (cauhoi == null)
                {
                    timerClock.Enabled = false;
                    return;
                }
                tbcontent.Text = cauhoi.CauHoi1.Trim();
                lblContent.Content = tbcontent;
            }
        }
        void frmSection2Start_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            filename = "section" + 2 + "childlayout.xml";
            timerClock = new System.Windows.Forms.Timer();
            timerClock.Tick += new EventHandler(timerClock_Tick);
            timerClock.Interval = 1000;
            cauhoi = CauHoiDAO.getCauHoiTrinhChieu(MainWindow.getCuocThiChon(), 2, goicauhoi);
            if (cauhoi == null)
            {
                MessageBox.Show("Không có câu hỏi");
            }
            timerClock.Enabled = false;
            Generate();
        }

        void timerClock_Tick(object sender, EventArgs e)
        {
            timetick--;
            if (timetick >= 0)
            {
                lblClock.Content = timetick;
            }
            else
            {
                timerClock.Enabled = false;
            }
        }
        string filename;
        LabelLayout lblLayoutClock;
        int timetick;
        Label lblClock;
        CauHoi cauhoi;
        Label lblContent;
        TextBlock tbcontent;
        void Generate()
        {
            Button btnHome;
            
            Control cbtnHome;
            btnHome = new Button();
            cbtnHome = (Control)btnHome;
            LabelLayout lableXML;
            lableXML = Helper.getButtonFromXML(filename, "HomeButton");
            AssignControl.assignControlLayout(ref cbtnHome, lableXML, "btnHome");
            ImageBrush myBrush = new ImageBrush();
            myBrush.ImageSource =
                   new BitmapImage(new Uri(ConfigXMLDAO.getHomeButtonUrl(), UriKind.RelativeOrAbsolute));
            btnHome.Background = myBrush;
            btnHome.Click += new System.Windows.RoutedEventHandler(btnHome_Click);
            gridMain.Children.Add(btnHome);

            //phan noi dung
            //title
            Label lblTitle = new Label();
            Control clblTitle = (Control)lblTitle;
            lableXML = Helper.getLabelFromXML(filename, "Title");
            AssignControl.assignControlLayout(ref clblTitle, lableXML, "lblTitle");
            lblTitle.FontSize = lableXML.FontSize;
            lblTitle.FontWeight = System.Windows.FontWeights.Bold;
            lblTitle.Foreground = lableXML.Foreground;
            lblTitle.Background = lableXML.Background;

            lblTitle.Content = "Gói Câu hỏi " + goicauhoi;


            gridMain.Children.Add(lblTitle);
            
            //content
            lblContent = new Label();
            Control clblContent = (Control)lblContent;
            lableXML = Helper.getLabelFromXML(filename, "Content");
            AssignControl.assignControlLayout(ref clblContent, lableXML, "lblContent");
            lblContent.FontSize = lableXML.FontSize;
            lblContent.FontWeight = System.Windows.FontWeights.Bold;
            lblContent.Foreground = lableXML.Foreground;
            lblContent.Background = lableXML.Background;
            tbcontent = new TextBlock();
            tbcontent.TextWrapping = TextWrapping.Wrap;
            tbcontent.FontSize = lableXML.FontSize;
            //tbcontent.Text = cauhoi.CauHoi1;
            //lblContent.Content = tbcontent;

            gridMain.Children.Add(lblContent);
            //clock
            lblClock = new Label();
            Control clblClock = (Control)lblClock;
            lblLayoutClock = Helper.getClockFromXML(filename);
            AssignControl.assignControlLayout(ref clblClock, lblLayoutClock, "lblClock");
            lblClock.VerticalContentAlignment = System.Windows.VerticalAlignment.Center;
            lblClock.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Center;
            lblClock.FontSize = lblLayoutClock.FontSize;
            lblClock.FontWeight = System.Windows.FontWeights.Bold;
            lblClock.Style = (Style)this.FindResource("PulseLabel");
            lblClock.Foreground = lblLayoutClock.Foreground;
            lblClock.Content = lblLayoutClock.iTime1;
            DrawingBrush myDrawingBrush = new DrawingBrush();
            GeometryDrawing myGeometryDrawing = new GeometryDrawing();
            myGeometryDrawing.Brush = lblLayoutClock.Background;

            myGeometryDrawing.Pen = new Pen(Brushes.Red, 1);
            GeometryGroup ellipses = new GeometryGroup();
            ellipses.Children.Add(new EllipseGeometry(new Point(25, 50), 50, 50));
            myGeometryDrawing.Geometry = ellipses;
            myDrawingBrush.Drawing = myGeometryDrawing;
            lblClock.Background = myDrawingBrush;
            lblClock.MouseDoubleClick += new MouseButtonEventHandler(lblClock_MouseDoubleClick);
            timetick = lblLayoutClock.iTime1;
            //timerClock.Enabled = true;
            //timerClock.Start();
            gridMain.Children.Add(lblClock);



        }
        void lblClock_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //nguoi thu 2
            timerClock.Enabled = false;
            lblClock.Foreground = lblLayoutClock.Foreground2;
            lblClock.Content = lblLayoutClock.iTime2;
            DrawingBrush myDrawingBrush = new DrawingBrush();
            GeometryDrawing myGeometryDrawing = new GeometryDrawing();
            myGeometryDrawing.Brush = lblLayoutClock.Background2;

            myGeometryDrawing.Pen = new Pen(Brushes.Red, 1);
            GeometryGroup ellipses = new GeometryGroup();
            ellipses.Children.Add(new EllipseGeometry(new Point(5, 5), 25, 25));
            myGeometryDrawing.Geometry = ellipses;
            myDrawingBrush.Drawing = myGeometryDrawing;
            lblClock.Background = myDrawingBrush;
            timetick = lblLayoutClock.iTime2;
            timerClock.Enabled = true;
            timerClock.Start();
        }

        System.Windows.Forms.Timer timerClock;
        void btnHome_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.Close();
        }

        void frmSection2Start_Closed(object sender, EventArgs e)
        {
            instance = null;
        }

    }
}
